﻿using Distributed.RedisSession.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Distributed.RedisSession.Controllers
{
    //dotnet Distributed.RedisSession.dll --urls=http://*:5010 --ip=127.0.0.1 --port=5010
    //dotnet Distributed.RedisSession.dll --urls=http://*:5011 --ip=127.0.0.1 --port=5011

    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private IConfiguration configuration;
        private IDistributedCache DistributedCach;
        private IMemoryCache MemoryCach;
        public HomeController(ILogger<HomeController> logger, IConfiguration configuration, IDistributedCache distributedCache,IMemoryCache memoryCach)
        {
            _logger = logger;
            this.configuration = configuration;
            this.DistributedCach = distributedCache;
            this.MemoryCach = memoryCach;
        }

        //[ResponseCache(Duration =120)]  //和下面的是一样的
        public IActionResult Index()
        {
            //base.HttpContext.Response.Headers["Cache-Control"] = "public,max-age=120";

            base.ViewBag.BrowserUrl = $"{base.Request.Scheme}://{base.Request.Host.Host}:{base.Request.Host.Port}";
            base.ViewBag.AppUrl = $"{base.Request.Scheme}://{base.Request.Host.Host}:{configuration["port"]}";
            base.ViewBag.TotalCount += 1;

            #region session
           
            string user = base.HttpContext.Session.GetString("CurrentUser");
            if(string.IsNullOrWhiteSpace(user))
            {
                base.HttpContext.Session.SetString("CurrentUser", configuration["port"]);

            }

            base.ViewBag.SessionUser = base.HttpContext.Session.GetString("CurrentUser");
            #endregion
            return View();
        }


        public IActionResult MemCache()
        {
            base.ViewBag.BrowserUrl = $"{base.Request.Scheme}://{base.Request.Host.Host}:{base.Request.Host.Port}";
            base.ViewBag.AppUrl = $"{base.Request.Scheme}://{base.Request.Host.Host}:{configuration["port"]}";
            base.ViewBag.TotalCount += 1;

            #region session
            //dotnet Distributed.RedisSession.dll --urls=http://*:5010 --ip=127.0.0.1 --port=5010
            //dotnet Distributed.RedisSession.dll --urls=http://*:5011 --ip=127.0.0.1 --port=5011
            string user = base.HttpContext.Session.GetString("CurrentUser");
            if (string.IsNullOrWhiteSpace(user))
            {
                base.HttpContext.Session.SetString("CurrentUser", configuration["port"]);

            }

            base.ViewBag.SessionUser = base.HttpContext.Session.GetString("CurrentUser");
            #endregion

            #region 内存缓存
            string key = "time_info";

            string time = this.MemoryCach.Get<string>(key);
            if (!string.IsNullOrWhiteSpace(time))
            {
            }
            else
            {
                time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
                this.MemoryCach.Set(key, time);
            }
            base.ViewBag.DistributedCachNow = time;
            base.ViewBag.nowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
            #endregion
            return View();
        }

        public IActionResult RedisCache()
        {
            
            base.ViewBag.BrowserUrl = $"{base.Request.Scheme}://{base.Request.Host.Host}:{base.Request.Host.Port}";
            base.ViewBag.AppUrl = $"{base.Request.Scheme}://{base.Request.Host.Host}:{configuration["port"]}";
            base.ViewBag.TotalCount += 1;

            #region session           
            string user = base.HttpContext.Session.GetString("CurrentUser");
            if (string.IsNullOrWhiteSpace(user))
            {
                base.HttpContext.Session.SetString("CurrentUser", configuration["port"]);

            }

            base.ViewBag.SessionUser = base.HttpContext.Session.GetString("CurrentUser");
            #endregion

            #region 分布式缓存
            string key = "time_info";

            string time = this.DistributedCach.GetString(key);
            if (!string.IsNullOrWhiteSpace(time))
            {
            }
            else
            {
                time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
                this.DistributedCach.SetString(key, time,new DistributedCacheEntryOptions() {  AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(120)});
            }
            base.ViewBag.DistributedCachNow = time;
            base.ViewBag.nowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
            #endregion
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
